package com.group.flagtivities;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;

public class Search extends Activity implements OnClickListener, TextWatcher {
	
	private static EditText searchCriteria;
	private static LinearLayout searchResults;
	
	private ArrayList<FlagtivityObject> results;
	private Context mContext;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search);
		
		findViews();
		configure();
		
		TextUtil.setFont((ViewGroup) findViewById(R.id.search), getAssets());
	}
	
	private void findViews() {
		searchResults = (LinearLayout) findViewById(R.id.searchResults);
		searchCriteria = (EditText) findViewById(R.id.searchCriteria);
	}
	
	private void configure() {
		searchCriteria.addTextChangedListener(this);
		mContext = this;
	}
	
	private void configureFlag(TextView view, int type) {
		Drawable drawable = null;
		
		switch(type) {
		case 1:
			drawable = ImageUtil.colorSwatch(40, 40, getResources().getColor(R.color.red_light), getResources().getColor(R.color.red_dark));
			break;
		case 2:
			drawable = ImageUtil.colorSwatch(40, 40, getResources().getColor(R.color.orange_light), getResources().getColor(R.color.orange_dark));
			break;
		case 3:
			drawable = ImageUtil.colorSwatch(40, 40, getResources().getColor(R.color.yellow_light), getResources().getColor(R.color.yellow_dark));
			break;
		case 4:
			drawable = ImageUtil.colorSwatch(40, 40, getResources().getColor(R.color.green_light), getResources().getColor(R.color.green_dark));
			break;
		case 5:
			drawable = ImageUtil.colorSwatch(40, 40, getResources().getColor(R.color.blue_light), getResources().getColor(R.color.blue_dark));
			break;
		case 6:
			drawable = ImageUtil.colorSwatch(40, 40, getResources().getColor(R.color.indigo_light), getResources().getColor(R.color.indigo_dark));
			break;
		case 7:
			drawable = ImageUtil.colorSwatch(40, 40, getResources().getColor(R.color.violet_light), getResources().getColor(R.color.violet_dark));
			break;
		}
        
		if (drawable != null) {
			drawable.setBounds(0, 0, 40, 40);
			view.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
        	view.setCompoundDrawablePadding(10);
		}
	}
	
	private void populateSearchResults() {
		results = new ArrayList<FlagtivityObject>();
		searchResults.removeAllViews();
		
		String criteria = searchCriteria.getText().toString().trim();
		
		if (!criteria.isEmpty()) {
			for (FlagtivityObject flag : Flagtivities.FlagtivityList) {
				if (flag.title.toLowerCase().contains(criteria.toLowerCase())) {
					results.add(flag);
				}
			}
			
			for (FlagtivityObject flag : results) {
				createResult(flag);
			}
			
			if (results.size() == 0) {
				TextView view = ViewUtil.generateDefaultTextView(this, "No Flagtivities found.");
				view.setGravity(Gravity.CENTER);
				searchResults.addView(view);
			}
		}
	}

	private void createResult(final FlagtivityObject flag) {
		RelativeLayout r = ViewUtil.generateDefaultRelativeLayout(this, getResources().getDrawable(R.drawable.background_recent));
		TextView v = ViewUtil.generateDefaultTextView(this, flag.title);
		ImageView map = ViewUtil.generateDefaultImageView(this, getResources().getDrawable(R.drawable.map));
		
		configureFlag(v, flag.type);
		
		v.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {
				Bundle bundle = BundleUtil.generateDetailBundle(flag.type, flag.activity_id, flag.owner, flag.description, flag.room, flag.title, flag.start, flag.end, flag.dislike, flag.like, flag.attending);
				Intent intent = new Intent(mContext, Detail.class);
				intent.putExtra("Details", bundle);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				mContext.startActivity(intent);
			}
		});
			
		map.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Flagtivities.mapController.setCenter(new GeoPoint(flag.lat, flag.lon));
				finish();
			}
		});

		r.addView(v);
		r.addView(map);
		searchResults.addView(r);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.close:
			finish();
			break;
		}
	}

	@Override
	public void afterTextChanged(Editable s) {}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		populateSearchResults();
	}
	
}
